
<div class="layuimini-container">
<div class="layui-row">
    <form id="app-form" class="layui-form layuimini-form" autocomplate="off">
	<div class="layui-card">
			<div class="layui-card-header">
				<i class="fa fa-fire icon"></i> 新增商机
			</div>
			<div class="layui-card-body">
				<div class="layui-form-item">
					<label class="layui-form-label">商机名称：</label>
					<div class="layui-input-inline">
						<input type="text" name="name" lay-verify="required" lay-reqtext="商机名称不能为空" placeholder="请输入商机名称"
							value="" class="layui-input field-name">
					</div>
				
					<label class="layui-form-label">客户名称：</label>
					<div class="layui-input-inline">
						<div id="customer" class="xm-select-demo"></div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">预计成交时间：</label>
					<div class="layui-input-inline">
						<input type="text" name="deal_time" class="layui-input field-deal_time" data-date>
					</div>
					<label class="layui-form-label">下次跟进时间：</label>
					<div class="layui-input-inline">
						<input type="text" name="next_time" class="layui-input field-next_time" data-date>
					</div>
					<div class="layui-form-mid layui-word-aux">日期面板左下角可选择跟进具体时间</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">备注：</label>
					<div class="layui-input-block">
					  <textarea name="remark" placeholder="请输入内容" class="layui-textarea field-remark"></textarea>
					</div>
				</div>
				<input type="hidden" class="field-id" name="id">
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</div>
	</div>	
    
    </form>
</div>
<div class="layui-row">
	<div class="layui-col-md12">
		<div class="layui-card">
			<div class="layui-card-header">
				<i class="fa fa-fire icon"></i> 产品信息列表
			</div>
			<div class="layui-card-body">
				<table class="layui-hide" id="productTableId" lay-filter="product"></table>
			</div>
		</div>	
	</div>
	
	<script type="text/html" id="toolbarDemo">
	  <div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm" lay-event="add">添加商品</button>
	  </div>
	</script>
	<script type="text/html" id="toolEventDemo">
	  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>
</div>
</div>

<script>
var customer = xmSelect.render({
	el: '#customer', 
	name:'customer_id',
	radio:true,
	filterable: true,
	data: JSON.parse('{:json_encode($customerOwnerList)}')
})
</script>
<script>
    layui.use(['jquery','table','form'], function () {
        var $ = layui.jquery
		,form = layui.form
        ,table = layui.table;

        var tableContent = new Array();
        table.render({
            elem: '#productTableId'
            ,url: 'businessProduct'
            ,toolbar: '#toolbarDemo'
			,defaultToolbar: ['filter']
            ,cols: [[
                { field: 'id', width: 80, title: '序号', sort: true },
                { field: 'product_id', width: 150, title: '商品编码' },
                { field: 'price', title: '价格', width: 130, sort: true },
                { field: 'nums', width: 100, title: '数量' },
                { field: 'subtotal', width: 100, title: '小计' }, 
				{ field: 'remark', title: '备注' },
                { title: '操作', width: 160, toolbar: '#toolEventDemo', align: "center", fixed: 'right' }
            ]]
            ,done: function (res, curr, count) {
                var dity = 0;
                if (layui.table.getData('currentTableId').length > 0) {
                    for (var i in layui.table.getData('currentTableId')) {
                        dity += parseFloat($(".layui-table-box tbody tr[data-index='" + i + "'] td[data-field='price'] .layui-table-cell").text())
                    }
                }
                $("#subal").val(dity);
                $("#total").text(dity);
                $('#discount').keyup(function () {
                    var discounts = $(this).val();
                    var fina = dity * (discounts * 0.01);
                    $("#subal").val((fina));
                    if ($("#subal").val() == 0.00) {
                        $("#subal").val((dity));
                    }
                })
            }
        });

        var userId = '';
        table.on('toolbar(product)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    type: 2,
                    title: '商品信息',
                    area: ['80%', '80%'],
                    content: '/crm/business.product/change',
                    success: function (layero, index) {
                        var iframeWindow = window['layui-layer-iframe' + index]  //获取弹框页面
                        iframeWindow.layui.table.on('tool(product)', function (obj) {
                            var data = obj.data;
                            var _id = data['id'];
                            if (obj.event === 'add') {
                                userId += _id + ",";
                                $.get('colis?id=' + userId, function (res) {
                                    if (res.errcode != 1) {
                                        obj.del();
                                        layer.close(index);
                                        table.reload('currentTableId', {
                                            scrollPos: 'fixed'
                                        });
                                    }
                                })
                            }
                        });

                    },
                });
            }
        })

    });

</script>
</body>
</html>